package com.example.lj.uav.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lj.uav.MyApplication;
import com.example.lj.uav.R;
import com.example.lj.uav.utils.OnUIUtils;

import java.util.HashSet;

import dji.common.error.DJIError;
import dji.common.flightcontroller.CalibrationOrientation;
import dji.common.flightcontroller.imu.CalibrationState;
import dji.common.flightcontroller.imu.IMUState;
import dji.common.util.CommonCallbacks;
import dji.sdk.flightcontroller.FlightController;

public class IMUDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private FrameLayout fl_cancel;
    private LinearLayout ll_step1, ll_step2, ll_step3;
    private TextView tv_start, tv_state;
    private ProgressBar pb;
    private CalState cs_top, cs_right, cs_left, cs_front, cs_behind, cs_under;

    public IMUDialog(@NonNull Context context) {
        super(context, R.style.AppTheme);
        this.context = context;
    }

    public IMUDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.AppTheme);
        this.context = context;
    }

    protected IMUDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_imu);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        fl_cancel = findViewById(R.id.imu_fl_cancel);
        fl_cancel.setOnClickListener(this);

        ll_step1 = findViewById(R.id.imu_ll_step1);
        ll_step2 = findViewById(R.id.imu_ll_step2);
        tv_start = findViewById(R.id.imu_tv_start);
        tv_start.setOnClickListener(this);

        pb = findViewById(R.id.imu_pb);
        tv_state = findViewById(R.id.imu_tv_state);

        findViewById(R.id.imu_tv_step3_return).setOnClickListener(this);

        cs_top = new CalState(findViewById(R.id.imu_v_top), 4);
        cs_right = new CalState(findViewById(R.id.imu_v_right), 2);
        cs_left = new CalState(findViewById(R.id.imu_v_left), 3);
        cs_front = new CalState(findViewById(R.id.imu_v_front), 0);
        cs_behind = new CalState(findViewById(R.id.imu_v_behind), 1);
        cs_under = new CalState(findViewById(R.id.imu_v_under), 5);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imu_fl_cancel:
            case R.id.imu_tv_step3_return:
                dismiss();
                break;
            case R.id.imu_tv_start:
                startIMUCal();
                break;
        }
    }

    private void startIMUCal() {
        FlightController flightController = MyApplication.getFlightControllerInstance();
        if (flightController == null) {
            Toast.makeText(context, "启动失败", Toast.LENGTH_SHORT).show();
            return;
        }
        flightController.startIMUCalibration(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onResult(DJIError djiError) {
                if (djiError != null) {
                    OnUIUtils.setToast(djiError.getDescription());
                } else {
                    OnUIUtils.setViewVisibility(fl_cancel, View.GONE);
                    OnUIUtils.setViewVisibility(ll_step1, View.GONE);
                    OnUIUtils.setViewVisibility(ll_step2, View.VISIBLE);

                }
            }
        });
        flightController.setIMUStateCallback(new IMUState.Callback() {
            @Override
            public void onUpdate(@NonNull IMUState imuState) {
                if (ll_step2.getVisibility() == View.VISIBLE) {
                    CalibrationState calibrationState = imuState.getCalibrationState();

                    if (calibrationState.value() == CalibrationState.CALIBRATING.value()) {
                        int p = imuState.getCalibrationProgress();
                        if (p >= 1 && p <= 100) {
                            OnUIUtils.setProgressBarProgress(pb, p);
                            OnUIUtils.setTextView(tv_state, "正在校准，请稍候");
                        }
                    } else if (calibrationState.value() == CalibrationState.SUCCESSFUL.value()) {
                        showEndView(true);
                    } else if (calibrationState.value() == CalibrationState.FAILED.value()) {
                        showEndView(false);
                    } else if (calibrationState.value() == CalibrationState.UNKNOWN.value()) {
                        OnUIUtils.setViewVisibility(fl_cancel, View.VISIBLE);
                    } else if (calibrationState.value() == CalibrationState.NONE.value()) {
                        OnUIUtils.setViewVisibility(fl_cancel, View.VISIBLE);
                    }

                    HashSet<CalibrationOrientation> calibrate = imuState.getMultipleOrientationCalibrationHint().
                            getOrientationsToCalibrate();
                    for (CalibrationOrientation co : calibrate) {
                        switch (co.value()) {
                            case 4:
                                cs_top.setState(1);
                                break;
                            case 2:
                                cs_right.setState(1);
                                break;
                            case 3:
                                cs_left.setState(1);
                                break;
                            case 0:
                                cs_front.setState(1);
                                break;
                            case 1:
                                cs_behind.setState(1);
                                break;
                            case 5:
                                cs_under.setState(1);
                                break;
                        }
                    }

                    HashSet<CalibrationOrientation> calibrated = imuState.getMultipleOrientationCalibrationHint().
                            getOrientationsCalibrated();
                    for (CalibrationOrientation co : calibrated) {
                        switch (co.value()) {
                            case 4:
                                cs_top.setState(2);
                                break;
                            case 2:
                                cs_right.setState(2);
                                break;
                            case 3:
                                cs_left.setState(2);
                                break;
                            case 0:
                                cs_front.setState(2);
                                break;
                            case 1:
                                cs_behind.setState(2);
                                break;
                            case 5:
                                cs_under.setState(2);
                                break;
                        }
                    }
                }
            }
        });
    }

    private class CalState {

        private View view;
        private int orientation;

        private CalState(View view, int orientation) {
            this.view = view;
            this.orientation = orientation;
        }

        public int getOrientation() {
            return orientation;
        }

        private void setState(int state) {
            if (state == 2)
                OnUIUtils.setBackground(view,R.drawable.ic_point_green);
//                view.setBackgroundResource(R.drawable.ic_point_green);
            else if (state == 1) {
                OnUIUtils.setBackground(view,R.drawable.ic_point_blue);
//                view.setBackgroundResource(R.drawable.ic_point_blue);
                findViewById(R.id.imu_v_pic).setBackgroundResource(getRes(orientation));
            } else
                OnUIUtils.setBackground(view,R.drawable.ic_point_gray);
//                view.setBackgroundResource(R.drawable.ic_point_gray);
        }

        private int getRes(int orientation) {
            int res = 4;
            switch (orientation) {
                case 4:
                    res = R.drawable.setting_ui_imu_top_m210;
                    break;
                case 2:
                    res = R.drawable.setting_ui_imu_right_m210;
                    break;
                case 3:
                    res = R.drawable.setting_ui_imu_left_m210;
                    break;
                case 0:
                    res = R.drawable.setting_ui_imu_front_m210;
                    break;
                case 1:
                    res = R.drawable.setting_ui_imu_behind_m210;
                    break;
                case 5:
                    res = R.drawable.setting_ui_imu_under_n3;
                    break;
            }
            return res;
        }
    }

    private void showEndView(boolean isSuc) {
        OnUIUtils.setViewVisibility(fl_cancel, View.VISIBLE);
        OnUIUtils.setViewVisibility(ll_step2, View.GONE);
        OnUIUtils.setViewVisibility(ll_step3, View.VISIBLE);
        OnUIUtils.setTextView((TextView) findViewById(R.id.imu_tv_step3_state),
                context.getResources().getString(R.string.imu_calibration_state, isSuc ? "成功" : "失败"));
        OnUIUtils.setViewVisibility(findViewById(R.id.imu_tv_step3_reboot),
                isSuc ? View.VISIBLE : View.GONE);
    }

}
